PIC24F
ADC Peripheral Module Library Help


Table Of Contents

1     Library Features. 3

2     Using Library Functions in Your Code. 3

3     Functions. 5

3.1     BusyADC10. 5

3.2     CloseADC10. 5

3.3     ConfigIntADC10. 5

3.4     ConvertADC10. 6

3.5     OpenADC10. 6

3.6     SetChanADC10. 8

3.7     ReadADC10. 8

3.8     StopSampADC1. 8

4     Macros. 9

4.1     EnableIntADC.. 9

4.2     DisableIntADC.. 9

4.3     SetPriorityIntADC.. 9

 


1          Library Features

 

This peripheral library module provides:

 

·         Up to 16 analog input pins, designated AN0 through AN15, on the 10-bit A/D converter, depending on the particular device pinout.

·         Two analog input pins for external voltage reference connections.

·         Voltage reference inputs that may be shared with other analog input pins.

 

2          Using Library Functions in Your Code

 

Library routine parameters can be constructed using either AND based mask or AND_OR based mask setting. For more information on these masks, see 16-bit Peripheral Libraries.

 

Examples of use for both the methods are below.

 

Example of Use ( AND mask )

 

#include<adc.h>

 

unsigned int Channel, PinConfig, Scanselect, Adcon3_reg, Adcon2_reg,

   Adcon1_reg;

int main(void)

{

   unsigned int result[20], i;

   ADCON1bits.ADON = 0; /* turn off ADC */

 

   Channel = ADC_CH0_POS_SAMPLEA_AN4 &

             ADC_CH0_NEG_SAMPLEA_NVREF &

             ADC_CH0_POS_SAMPLEB_AN2 &

             ADC_CH0_NEG_SAMPLEB_AN1;

 

   SetChanADC1(Channel);

 

   ConfigIntADC1(ADC_INT_DISABLE);

 

   PinConfig = ENABLE_AN4_ANA;

   Scanselect = ADC_SKIP_SCAN_AN2 & ADC_SKIP_SCAN_AN5 &

                ADC_SKIP_SCAN_AN9 & ADC_SKIP_SCAN_AN10 &

                ADC_SKIP_SCAN_AN14 & ADC_SKIP_SCAN_AN15;

 

   Adcon3_reg = ADC_SAMPLE_TIME_10 &

                ADC_CONV_CLK_SYSTEM &

                ADC_CONV_CLK_13Tcy;

 

   Adcon2_reg = ADC_VREF_AVDD_AVSS &

                ADC_SCAN_OFF &

                ADC_ALT_BUF_OFF &

                ADC_ALT_INPUT_OFF &

                ADC_CONVERT_CH0123 &

                ADC_SAMPLES_PER_INT_16;

 

   Adcon1_reg = ADC_MODULE_ON &

                ADC_IDLE_CONTINUE &

                ADC_FORMAT_INTG &

                ADC_CLK_MANUAL &

                ADC_SAMPLE_SIMULTANEOUS &

                ADC_AUTO_SAMPLING_OFF;

 

   OpenADC10(Adcon1_reg, Adcon2_reg,

             Adcon3_reg,PinConfig, Scanselect);

   i = 0;

 

   while(i <16 )

   {

      AD1CON1bits.SAMP = 1;

      while(!AD1CON1bits.SAMP);

      ConvertADC10();

      while(AD1CON1bits.SAMP);

      while(!BusyADC10());

      while(BusyADC10());

      result[i] = ReadADC10(i);

      i++;

   }

}

 

Example of Use ( AND_OR mask )

 

#define USE_AND_OR        /* To enable AND_OR mask setting */

#include<adc.h>

 

unsigned int Channel, PinConfig, Scanselect, Adcon3_reg, Adcon2_reg,

   Adcon1_reg;

 

int main(void)

{

   unsigned int result[20], i;

   ADCON1bits.ADON = 0; /* turn off ADC */

 

   Channel = ADC_CH0_POS_SAMPLEA_AN4 |

             ADC_CH0_NEG_SAMPLEA_NVREF |

             ADC_CH0_POS_SAMPLEB_AN2 |

             ADC_CH0_NEG_SAMPLEB_AN1;

 

   SetChanADC10(Channel);

 

   ConfigIntADC10(ADC_INT_DISABLE);

 

   PinConfig = ENABLE_AN4_ANA;

 

   Scanselect = ADC_SKIP_SCAN_AN2 | ADC_SKIP_SCAN_AN5 |

                ADC_SKIP_SCAN_AN9 | ADC_SKIP_SCAN_AN10 |

                ADC_SKIP_SCAN_AN14 | ADC_SKIP_SCAN_AN15;

 

   Adcon3_reg = ADC_SAMPLE_TIME_10 |

                ADC_CONV_CLK_SYSTEM |

                ADC_CONV_CLK_13Tcy;

 

   Adcon2_reg = ADC_VREF_AVDD_AVSS |

                ADC_SCAN_OFF |

                ADC_ALT_BUF_OFF |

                ADC_ALT_INPUT_OFF |

                ADC_CONVERT_CH0123 |

                ADC_SAMPLES_PER_INT_16;

 

   Adcon1_reg = ADC_MODULE_ON |

                ADC_IDLE_CONTINUE |

                ADC_FORMAT_INTG |

                ADC_CLK_MANUAL |

                ADC_SAMPLE_SIMULTANEOUS |

                ADC_AUTO_SAMPLING_OFF;

 

   OpenADC10(Adcon1_reg, Adcon2_reg,

             Adcon3_reg,PinConfig, Scanselect);

   i = 0;

   while(i <16 )

   {

      AD1CON1bits.SAMP = 1;

      while(!AD1CON1bits.SAMP);

      ConvertADC10();

      while(AD1CON1bits.SAMP);

      while(!BusyADC10());

      while(BusyADC10());

      result[i] = ReadADC10(i);

      i++;

   }

}

 

3          Functions

3.1         BusyADC10

 

Function Prototype

char BusyADC10(void);

Include

adc.h

Description

This function returns the ADC conversion status.

Arguments

None

Return Value

If the value of DONE is ‘0’, then ‘1’ is returned, indicating that the ADC is busy in conversion.

If the value of DONE is ‘1’, then ‘0’ is returned, indicating that the ADC has completed conversion.

Remarks:

This function returns the complement of the ADCON1 <DONE> bit status which indicates whether the ADC is busy in conversion.

3.2         CloseADC10

 

Function Prototype

void CloseADC10(void);

Include

adc.h

Description

This function turns off the ADC module and disables the ADC interrupts.

Arguments

None

Return Value

None

Remarks:

This function first disables the ADC interrupt and then turns off the ADC module. The Interrupt Flag bit (ADIF) is also cleared.

3.3         ConfigIntADC10

 

Function Prototype

void ConfigIntADC10(unsigned int config);

Include

adc.h

Description

This function configures the ADC interrupt.

Arguments

config - ADC interrupt priority and enable/disable information as defined below:

ADC Interrupt enable/disable

   ADC_INT_ENABLE

   ADC_INT_DISABLE

ADC Interrupt priority

   ADC_INT_PRI_0

   ADC_INT_PRI_1

   ADC_INT_PRI_2

   ADC_INT_PRI_3

   ADC_INT_PRI_4

   ADC_INT_PRI_5

   ADC_INT_PRI_6

   ADC_INT_PRI_7

Return Value

None

Remarks:

This function clears the Interrupt Flag (ADIF) bit and then sets the interrupt priority and enables/disables the interrupt.

 

3.4         ConvertADC10

 

Function Prototype

void ConvertADC10(void);

Include

adc.h

Description

This function starts the A/D conversion.

Arguments

None

Return Value

None

Remarks:

This function clears the ADCON1<SAMP> bit and thus stops sampling and starts conversion.

This happens only when trigger source for the A/D conversion is selected as Manual, by clearing the ADCON1 <SSRC> bits.

3.5         OpenADC10

 

Function Prototype

void OpenADC10(unsigned int config1,

   unsigned int config2,

   unsigned int config3,

   unsigned int configport,

   unsigned int configscan)

Include

adc.h

Description

This function starts the A/D conversion.

Arguments

config1 - This contains the parameters to be configured in the ADCON1 register as defined below:

Module On/Off

   ADC_MODULE_ON

   ADC_MODULE_OFF

Idle mode operation

   ADC_IDLE_CONTINUE

   ADC_IDLE_STOP

Result output format

   ADC_FORMAT_SIGN_FRACT

   ADC_FORMAT_FRACT

   ADC_FORMAT_SIGN_INT

   ADC_FORMAT_INTG

Conversion trigger source

   ADC_CLK_AUTO

   ADC_CLK_MPWM

   ADC_CLK_TMR

   ADC_CLK_INT0

   ADC_CLK_MANUAL

Auto sampling select

   ADC_AUTO_SAMPLING_ON

   ADC_AUTO_SAMPLING_OFF

Simultaneous Sampling

   ADC_SAMPLE_SIMULTANEOUS

   ADC_SAMPLE_INDIVIDUAL

Sample enable

   ADC_SAMP_ON

   ADC_SAMP_OFF

 

config2 - This contains the parameters to be configured in the ADCON2 register as defined below:

Voltage Reference

   ADC_VREF_AVDD_AVSS

   ADC_VREF_EXT_AVSS

   ADC_VREF_AVDD_EXT

   ADC_VREF_EXT_EXT

Scan selection

   ADC_SCAN_ON

   ADC_SCAN_OFF

Number of samples between interrupts

   ADC_SAMPLES_PER_INT_1

   ADC_SAMPLES_PER_INT_2

   .....

   ADC_SAMPLES_PER_INT_15

   ADC_SAMPLES_PER_INT_16

Buffer mode select

   ADC_ALT_BUF_ON

   ADC_ALT_BUF_OFF

Alternate Input Sample mode select

   ADC_ALT_INPUT_ON

   ADC_ALT_INPUT_OFF

 

config3 - This contains the parameters to be configured in the ADCON3 register as defined below:

Auto Sample Time bits

   ADC_SAMPLE_TIME_0

   ADC_SAMPLE_TIME_1

   .....

   ADC_SAMPLE_TIME_31

Conversion Clock Source select

   ADC_CONV_CLK_INTERNAL_RC

   ADC_CONV_CLK_SYSTEM

Conversion clock select

   ADC_CONV_CLK_1Tcy2

   ADC_CONV_CLK_1Tcy

   ADC_CONV_CLK_3Tcy2

   .....

   ADC_CONV_CLK_32Tcy

   .....

   ADC_CONV_CLK_255Tcy2

   ADC_CONV_CLK_128Tcy

 

configport - This contains the pin select to be configured into the ADPCFG register as defined below:

   ENABLE_ALL_ANA

   ENABLE_ALL_DIG

   ENABLE_AN0_ANA

   ENABLE_AN1_ANA

   ENABLE_AN2_ANA

   .....

   ENABLE_AN15_ANA

 

configscan - This contains the scan select parameter to be configured into the  ADCSSL register as defined below:

   ENABLE_ALL_INPUT_SCAN

   DISABLE_ALL_INPU_SCAN

   ENABLE_AN0_INPUT_SCAN

   ENABLE_AN1_INPUT_SCAN

   .....

   ENABLE_AN15_INPUT_SCAN

Return Value

None

Remarks:

This function configures the ADC for the following parameters: Operating mode, Sleep mode behavior, Data o/p format, Sample Clk Source, VREF source, No of samples/int, Buffer Fill mode, Alternate i/p sample mod, Auto sample time, Conv clock source, Conv Clock Select bits, Port Config Control bits.

3.6         SetChanADC10

 

Function Prototype

void SetChanADC10(unsigned int channel);

Include

adc.h

Description

This function sets the positive and negative inputs for the sample

multiplexers A and B.

Arguments

channel - This contains the input select parameter to be configured into the ADCHS register as defined below:

Channel 0 Negative Input Select for MUX B Multiplexer

   ADC_CH0_NEG_SAMPLEB_AN1

   ADC_CH0_NEG_SAMPLEB_VREFN

Channel 0 Negative Input Select for MUX A Multiplexer

   ADC_CH0_NEG_SAMPLEA_AN1

   ADC_CH0_NEG_SAMPLEA_VREFN

Channel 0 Positive Input Select for MUX B

   ADC_CH0_POS_SAMPLEB_AN0

   ADC_CH0_POS_SAMPLEB_AN1

.....

   ADC_CH0_POS_SAMPLEB_AN15

Channel 0 Positive Input Select for MUX A

   ADC_CH0_POS_SAMPLEA_AN0

   ADC_CH0_POS_SAMPLEA_AN1

   .....

   ADC_CH0_POS_SAMPLEA_AN15

Return Value

None

Remarks:

This function configures the inputs for sample multiplexers A and B by writing to ADCHS register.

3.7         ReadADC10

 

Function Prototype

unsigned int ReadADC10(unsigned char bufIndex);

Include

adc.h

Description

This function reads the ADC Buffer register which contains the conversion value.

Arguments

bufIndex - This is the ADC buffer number which is to be read.

Return Value

None

Remarks:

This function returns the contents of the ADC Buffer register. User should provide bufIndex value between ‘0’ to ‘15’ to ensure correct read of the ADCBUF0 to ADCBUFF.

3.8         StopSampADC1

 

Include

adc.h

Description

This function is identical to ConvertADC1, i.e.,

#define to ConvertADC10 in adc10.h.

 

4          Macros

 

4.1         EnableIntADC

 

Macro

EnableIntADC

Include

adc.h

Description

This macro enables the ADC interrupt.

Arguments

None

Remarks

This macro sets ADC Interrupt Enable bit of Interrupt Enable Control Register.

4.2         DisableIntADC

 

Macro

DisableIntADC

Include

adc.h

Description

This macro disables the ADC interrupt.

Arguments

None

Remarks

This macro clears ADC Interrupt Enable bit of Interrupt Enable Control register.

4.3         SetPriorityIntADC

 

Macro

SetPriorityIntADC

Include

adc.h

Description

This macro sets priority for ADC interrupt.

Arguments

priority

Remarks

This macro sets ADC Interrupt Priority bits of Interrupt Priority Control register.